/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.activity;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.SchedulesIntentHolder;
import lt.marsrutai.mobile.android.activity.intent.StopSearchIntentHolder;
import lt.marsrutai.mobile.android.content.StopSearchRecentSuggestionsProvider;
import lt.marsrutai.mobile.android.fragment.StopSearchFragment;
import lt.marsrutai.mobile.android.handler.menu.MenuHandler;
import lt.marsrutai.mobile.android.handler.menu.StopSearchMenuHandler;
import lt.marsrutai.mobile.android.util.StringUtils;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SearchRecentSuggestions;
import android.support.v4.app.ListFragment;

public class StopSearchActivity extends BaseActivity {

    /**
     * Stores previously entered search query
     */
    private String searchQuery;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // activity has been re-started - don't perform search
        if (savedInstanceState == null) {
            handleIntent(getIntent());
        }
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            if (!StringUtils.equals(query, StopSearchRecentSuggestionsProvider.QUERY_CLOSEST_STOPS)) {
                SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, StopSearchRecentSuggestionsProvider.AUTHORITY, StopSearchRecentSuggestionsProvider.MODE);
                suggestions.saveRecentQuery(query, null);
                
                // Save the user-entered query
                searchQuery = query;
            }
            
            StopSearchFragment fragment = (StopSearchFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
            if (fragment != null) {
                fragment.searchAgain(query);
            }
        }
    }

    @Override
    public int getMenuResource() {
        return R.menu.stop_search_menu;
    }

    @Override
    public MenuHandler getMenuHandler() {
        return new StopSearchMenuHandler();
    }

    @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
        switch (itemPosition) {
        case 1:
            StopSearchIntentHolder sourceIntentHolder = new StopSearchIntentHolder(getIntent());

            SchedulesIntentHolder intentHolder = new SchedulesIntentHolder(this);
            intentHolder.setScopeFolder(sourceIntentHolder.getScopeFolder());
            intentHolder.setScopeName(sourceIntentHolder.getScopeName());
            startActivity(intentHolder.getIntent());
            break;
        case 2:
            startActivity(new Intent(this, ScopesActivity.class));
            break;
        default:
            return false;
        }

        return false;
    }

    @Override
    public NavMenuData getNavMenuData() {
        StopSearchIntentHolder intentHolder = new StopSearchIntentHolder(getIntent());
        return new NavMenuData(new String[] { getString(R.string.stop_search), intentHolder.getScopeName(), getString(R.string.home) });
    }

    @Override
    public void setupView() {
        if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            ListFragment fragment = new StopSearchFragment();
            fragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, fragment).commit();
        }
    }
    
    @Override
    public boolean onSearchRequested() {
        // Display previously entered search query
        startSearch(searchQuery, false, null, false);
        
        return true;
    }

}
